﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Flame.Dlr;

namespace Flame.Reflection
{
    public class IntellisenseParsing
    {
        const string StringGroupName = "v";
        const string PartialStringGroupName = "pv";

        static string inglobeGroup(string name, string rstring) { return "(?<" + name + ">" + rstring + ")"; }
        public static string RegexStringBuilder(string v_, string v, string v_pv, string pv, string pv_) { return v_ + inglobeGroup(StringGroupName, v) + v_pv + inglobeGroup(PartialStringGroupName, pv) + pv_ + "$"; }

        Regex _regex = null;

        IntellisenseParsing(string regexText) { _regex = new Regex(regexText); }


        public static string RegexStringBuilder_Type(string prefix, string divisor, string name)
        {
            return prefix + inglobeGroup(StringGroupName, name) + divisor + inglobeGroup(PartialStringGroupName, name) + "$";
        }

        public static IntellisenseParsing RegexBuilder(string prefix, string divisor, string nameChars, string post = @"")
        {

            string name = nameChars + "+";
            return new IntellisenseParsing(inglobeGroup(StringGroupName, prefix + name + divisor + "(" + name + divisor + ")*") + inglobeGroup(PartialStringGroupName, nameChars + "*") + post + "$");
        }
        

        public void GetMatchResults(string text, out string svalue, out string spartialvalue)
        {
            Match m = _regex.Match(text);
            svalue = m.Groups[StringGroupName].Value;
            spartialvalue = m.Groups[PartialStringGroupName].Value;
        }

        /*static IntellisenseParsing _methodInstance = null;
        public static IntellisenseParsing Method
        {
            get
            {
                if(_methodInstance == null)  _methodInstance =  new IntellisenseParsing(IntellisenseParsing.RegexStringBuilder(@"", @"(\$)?[a-zA-Z0-9_\:\.\[\]\(\)]+", @"((\:\:)|\.)", @"[a-zA-Z0-9_]+", @"\([^\)]*"));
                return _methodInstance;
            }
        }

        static IntellisenseParsing _typeInstance = null;

        public static IntellisenseParsing Type
        {
            get
            {
                if (_typeInstance == null) _typeInstance = new IntellisenseParsing(IntellisenseParsing.RegexStringBuilder(@"", @"(\$)?[a-zA-Z0-9_\:\.\[\]\(\)]+", @"((\:\:)|\.)", @"[a-zA-Z0-9_]*", @""));
                return _typeInstance;

            }
        }*/
    }
}

